using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using GAS.Common;
using System.Data.OracleClient;
using GAS.Common.DataSets;
using GAS.Common.Managers;
using GAS.Common.DbAccess;
using System.Configuration;
using GAS.GASWebServices;

namespace GAS
{
	public partial class MainView : Form
	{
		private GASManager gasManager;
		private GASWebService webService;


		public MainView()
		{
			InitializeComponent();

			gasManager = new GASManager();
			webService = new GASWebService();
			comboConnectionType.SelectedIndex = 0;
		}

		private void buttonExecuteSql_Click(object sender, EventArgs e)
		{
			DataSet ds;
			DateTime initialTime;
			DateTime endTime;
			TimeSpan difTime;
			bool webServiceConnection;

			webServiceConnection = (comboConnectionType.SelectedIndex == 0) || (comboConnectionType.SelectedIndex == -1);

			this.Cursor = Cursors.WaitCursor;

			initialTime = DateTime.MinValue;
			endTime = DateTime.MinValue;

			initialTime = DateTime.Now;

			ds = null;
			try
			{
				if (webServiceConnection)
					ds = webService.ExecuteSql(textSql.Text);
				else
					ds = gasManager.ExecuteSql(textSql.Text);
			}
			catch (Exception ex)
			{
				endTime = DateTime.Now;
				MessageBox.Show("Error while executing SQL query: " + ex.Message);
			}

			endTime = DateTime.Now;

			if ((ds != null) && (ds.Tables.Count > 0))
			{
				dataGridView1.DataSource = ds.Tables[0];
				labelRowNumber.Text = ds.Tables[0].Rows.Count.ToString() + " rows";
			}

			if (initialTime != DateTime.MinValue && endTime != DateTime.MinValue)
			{
				difTime = endTime - initialTime;
				labelQueryTime.Text = difTime.Milliseconds + " ms";
			}
			else
				labelQueryTime.Text = "? ms";

			this.Cursor = Cursors.Default;
		}

		private void nuevoToolStripButton_Click(object sender, EventArgs e)
		{
			AuditOptionsView auditOptionsView;
			bool webServiceConnection;

			webServiceConnection = (comboConnectionType.SelectedIndex == 0) || (comboConnectionType.SelectedIndex == -1);

			auditOptionsView = new AuditOptionsView(webServiceConnection);

			if (auditOptionsView.ShowDialog() == DialogResult.OK)
				MessageBox.Show("Your new audit is saved.");
		}

		private void abrirToolStripButton_Click(object sender, EventArgs e)
		{
			AuditDataSet auditDS;
			GASManager manager;
			string auditCode;

			openFileDialog1.Filter = "XML files|*.xml";
			if (openFileDialog1.ShowDialog() == DialogResult.OK)
			{
				try
				{
					auditDS = new AuditDataSet();
					auditDS.ReadXml(openFileDialog1.FileName, XmlReadMode.IgnoreSchema);

					manager = new GASManager();
					auditCode = manager.GetLastAuditCode();
					manager.SaveHostAudit(auditDS, auditCode);
				}
				catch (Exception ex)
				{
					MessageBox.Show("Error while saving file in DB. Details: " + ex.Message);
				}
			}
		}

		private void guardarToolStripButton_Click(object sender, EventArgs e)
		{
			saveFileDialog1.Filter = "XML files|*.xml";
			if (saveFileDialog1.ShowDialog() == DialogResult.OK)
			{
				if (dataGridView1.DataSource is DataTable)
				{
					DataSet ds;

					ds = ((DataTable)dataGridView1.DataSource).DataSet;
					ds.WriteXml(saveFileDialog1.FileName, XmlWriteMode.IgnoreSchema);
				}
				else
					MessageBox.Show("There is no results to save");
			}
		}

		private void ayudaToolStripButton_Click(object sender, EventArgs e)
		{
			About about;

			about = new About();
			about.ShowDialog();
		}
	}
}